package com.nlp.server.config;

import com.nlp.server.factory.MyThreadFactory;
import com.nlp.server.properties.ThreadPoolProperties;
import io.netty.util.concurrent.ThreadProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置类
 * @author shiliuyinzhen
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * 默认线程池配置
     * @return
     */
    @ConfigurationProperties(prefix = "default.pool")
    @Bean("defaultThreadPoolProperties")
    public ThreadPoolProperties defaultThreadProperties() {
        return new ThreadPoolProperties();
    }

    /**
     * sse连接线程池配置
     * @return
     */
    @ConfigurationProperties(prefix = "sse.pool")
    @Bean("sseThreadPoolProperties")
    public ThreadPoolProperties sseThreadProperties() {
        return new ThreadPoolProperties();
    }


    /**
     * 对话线程池配置
     * @return
     */
    @ConfigurationProperties(prefix = "chat.pool")
    @Bean("chatThreadPoolProperties")
    public ThreadPoolProperties chatThreadProperties() {
        return new ThreadPoolProperties();
    }

    /**
     * 默认线程池
     * @param properties
     * @return
     */
    @Bean("defaultExecutorService")
    public ExecutorService defaultExecutorService(@Qualifier("defaultThreadPoolProperties") ThreadPoolProperties properties) {
        return new ThreadPoolExecutor(properties.getCorePoolSize(), properties.getMaxPoolSize()
                , properties.getKeepAlive(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(properties.getQueueSize())
                , new MyThreadFactory(properties.getNamePrefix()) ,new ThreadPoolExecutor.CallerRunsPolicy());
    }

    /**
     * sse连接线程池，异步检查sse连接是否存活
     * @param properties
     * @return
     */
    @Bean("sseExecutorService")
    public ExecutorService sseExecutorService(@Qualifier("sseThreadPoolProperties") ThreadPoolProperties properties) {
        return new ThreadPoolExecutor(properties.getCorePoolSize(), properties.getMaxPoolSize()
                , properties.getKeepAlive(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(properties.getQueueSize())
                , new MyThreadFactory(properties.getNamePrefix()) ,new ThreadPoolExecutor.CallerRunsPolicy());
    }

    /**
     * 对话线程池，用于执行和大模型交互
     * @param properties
     * @return
     */
    @Bean("chatExecutorService")
    public ExecutorService chatExecutorService(@Qualifier("chatThreadPoolProperties") ThreadPoolProperties properties) {
        return new ThreadPoolExecutor(properties.getCorePoolSize(), properties.getMaxPoolSize()
                , properties.getKeepAlive(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(properties.getQueueSize())
                , new MyThreadFactory(properties.getNamePrefix()) ,new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
